/********************************************************************
* Copyright (c) 2014,TGS,All rights reserved.
* fileName:	LayerControlWidget.h	
* created:	2014/04/06 9:46
* author:	TGS
* version:	1.0
*********************************************************************/

#pragma once

#include <QTreeWidget>
#include <QIcon>
#include <QAction>
#include <QList>
#include <map>

namespace TGS{
namespace MapControl{
	class MapWidget;
}
}

class MapLayer;
class VectorAttributeTable;
class SelectedFeatureListDlg;
class DrawedPolygonDlg;
class VectorLayer;
class RasterLayer;

class LayerControlWidget : public QTreeWidget
{
	Q_OBJECT
	typedef TGS::MapControl::MapWidget MapWidget;
public:
	LayerControlWidget(MapWidget* map, QWidget *parent = nullptr);
	~LayerControlWidget(void);
public:
	void refresh();
	void addLayer( MapLayer* layer );
	void showCurrentVisibleSelectedFeatureListDlg();
	SelectedFeatureListDlg* getCurrentVisibleSelectedFeatureListDlg();
	DrawedPolygonDlg* getCurrentVisibleDrawedPolygonDlg();
protected:
	virtual void contextMenuEvent ( QContextMenuEvent * event );
	virtual void mousePressEvent( QMouseEvent * e );
	virtual void mouseMoveEvent( QMouseEvent * e );
	virtual void mouseReleaseEvent( QMouseEvent * e );
	virtual void mouseDoubleClickEvent( QMouseEvent* e );
private slots:
	void itemClicked( QTreeWidgetItem * item, int column );
	MapLayer* getMapLayer(QTreeWidgetItem * item);
	void deleteCurrentLayer();
	void showProperty();
	void showAttribute();
	void showSelectedFeatureListDlg();
	void showDrawedPolygonDlg();
private:
	void hideLine();
	void showLine( int y, int left );
	void updateLineWidget();
	QTreeWidgetItem * lastVisibleItem();
	QTreeWidgetItem* previousSibling( QTreeWidgetItem* item );
	void moveItem( QTreeWidgetItem* move, QTreeWidgetItem* after );
	void reorderMapLayer();
	
private:
	MapWidget*	_map;
	QIcon		_rasterIcon;
	QIcon		_vectorIcon;
	QAction*	_actionDelete;
	QAction*	_actionProperty;
	QAction*	_actionVectorAttribute;
	QAction*	_actionSelectedFeatureListDlg;
	QAction*	_actionDrawedPolygonDlg;
	QWidget*	_insertionLine;
	bool		_isLeftMouseDown;
	QTreeWidgetItem* _dropTarget;
	QList< QTreeWidgetItem * > _itemsBeingMoved;
	std::map<VectorLayer*, VectorAttributeTable*> _vectorAttribute;
	std::map<VectorLayer*, SelectedFeatureListDlg*> _selectedFeatureDlgs;
	std::map<RasterLayer*, DrawedPolygonDlg*> _drawedPolygonDlg;
	enum { BEFORE, INSERT, AFTER } _dropAction;
};

